
local U = require "packages/hanqing/util"

local jieqian = fk.CreateSkill{
  name = "hanqing__jieqian&",
  tags = { U.VerticalAlliance },
}

Fk:loadTranslationTable {
  ["hanqing__jieqian&"] = "解箝",
  [":hanqing__jieqian&"] = "<a href='hanqing__alliance_skill'>合纵技</a>，"..
  "出牌阶段限一次，你可以将一张装备牌置于其他角色的装备区（可替换原装备），"..
  "然后攻击范围内角色数因此变少的角色各回复1点体力。",
}

jieqian:addEffect("active", {
  anim_type = "support",
  prompt = "#hanqing__jieqian",
  card_num = 1,
  target_num = 1,
  can_use = function (self, player)
    return player:usedEffectTimes("hanqing__jieqian", Player.HistoryPhase) == 0 and U.getAlliance(player) == "vertical"
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and #selected_cards == 1 and to_select ~= player and
      to_select:canMoveCardIntoEquip(selected_cards[1], true)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    player:addSkillUseHistory("hanqing__jieqian", 1)
    local nums = table.map(room.players, function (p)
      return #table.filter(room.alive_players, function (q)
        return p:inMyAttackRange(q)
      end)
    end)
    room:moveCardIntoEquip(target, effect.cards[1], jieqian.name, true, player)
    local nums2 = table.map(room.players, function (p)
      return #table.filter(room.alive_players, function (q)
        return p:inMyAttackRange(q)
      end)
    end)
    for _, p in ipairs(room:getAlivePlayers()) do
      if not p.dead and nums[table.indexOf(room.players, p)] > nums2[table.indexOf(room.players, p)] then
        room:recover{
          who = p,
          num = 1,
          recoverBy = player,
          skillName = "hanqing__jieqian",
        }
      end
    end
  end,
})

return jieqian
